var HuangLi = {};
var calData = new Array();
var currentDate = new Date();
var calander = {
	//initialization the calender
	init:function()
	{
		//init初始化的功能只需要初始化一次
		makeCal.initAction();
		makeCal.makeHuangli(currentDate);
		$.getScript("http://utility.hao123.com/time.php?callback=clock");
	},
	//make the calender of `date`
	//init初始化的功能只需要初始化一次
	initAction:function()
	{
		$('#today_button').bind('click', function(e){
			makeCal.showToday();
		});

		var indiv = false;
		var inhuangli = false;
		$('#huangliDiv').bind('mouseover', function(e){
			inhuangli = true;
			setTimeout(function(){
				if ( indiv || inhuangli )
				$('#huangliDiv').css({'display': 'block'});
				}, 500);
		});
		$('#huangliDiv').bind('mouseout', function(e){
			inhuangli = false;
			if ( indiv == false )
			{
			setTimeout(function(){
				if ( inhuangli == false && indiv == false )
				$('#huangliDiv').css({'display': 'none'});
				}, 500);
			}
		});
	},
	//make初始化的功能每次重绘table后就要初始化一次
	
	//生成黄历div
	makeHuangli:function(date)
	{
		currentDate=date;
		date = makeCal.setTimeZero(date);
		var datestr = date.getDate();
		lunar = templates.lunar_Info_detail(date);
		$('#right_big_date').text(datestr);
		var gregorianDayStr = date.getFullYear()+"年"+(date.getMonth()+1)+"月 ";
		
		switch (date.getDay())
		{
			case 1:
				gregorianDayStr += '星期一';
				break;
			case 2:
				gregorianDayStr += '星期二';
				break;
			case 3:
				gregorianDayStr += '星期三';
				break;
			case 4:
				gregorianDayStr += '星期四';
				break;
			case 5:
				gregorianDayStr += '星期五';
				break;
			case 6:
				gregorianDayStr += '星期六';
				break;
			case 0:
				gregorianDayStr += '星期日';
				break;			
		}
		gregorianDayStr += " ";
		gregorianDayStr += getYearWeek(date);
		gregorianDayStr += "周 ";
		$('#gregorianDayStr').text(gregorianDayStr);
		$('#popChineseStr').text((lunar.lunar).substring(2));
		var nowDate = makeCal.setTimeZero(new Date());
		var nowMiliSecond = nowDate.getTime();
		var targetMiliSecond = date.getTime();
		var passedTime = Math.ceil((targetMiliSecond - nowMiliSecond)/86400000);
		var dayafterorbeforeStr = "";
		$('#dayafterorbefore').text(dayafterorbeforeStr);
		$('#chinaDay').text((lunar.lunar).substring(2));
		
		/*kun:过了春节就算龙年
		if(date.getFullYear()==2012 && (date.getMonth()==0 || (date.getMonth()==1 && date.getDate()<4))){
			lunar.y_Info=lunar.y_Info.replace("龙","兔");
		}
		*/
		//1.23日春节前显示为兔年
		if(date.getFullYear()==2012 && (date.getMonth()==0 && date.getDate()<23)){
			lunar.y_Info=lunar.y_Info.replace("龙","兔");
		}
		//2013 1 1~3013 2 9显示为龙年
		if(date.getFullYear()==2013 && (date.getMonth()==0 ||(date.getMonth()==1 && date.getDate()<10))){
			lunar.y_Info=lunar.y_Info.replace("蛇","龙");
		}
		info = lunar.y_Info;
		var yInfo = info.split(" ");
		$('#chinaDay2').text(yInfo[0]);
		$('#chinaDay3').text(yInfo[1]);
		
	},


	//set the time of date zero
	setTimeZero:function(date)
	{
		ndate = new Date(date);
		ndate.setHours(0);
		ndate.setMinutes(0);
		ndate.setSeconds(0);
		ndate.setMilliseconds(0);	
		return ndate;
	},
};



var templates = {
	month_day : function(date){
		var d = date || new Date();
		return d.getDate();
	},
	lunar_Info : function(date){
		var cld = cacheMgr.getCld(date.getFullYear(), date.getMonth());
		var day = date.getDate();
		var cld_day = cld[day - 1];
		var lunar_detail = {
			l_day : "",
			l_month : "",
			l_day_full:""
		};
		lunar_detail.l_day = cDay(cld_day.lDay);
		lunar_detail.l_month = cld_day.lMonth;
		lunar_detail.color = "";
		var s,s2;
		s=cld_day.lunarFestival;
		return lunar_detail;
	},
	lunar_Info_detail : function(date, callback){
		var cld = cacheMgr.getCld(date.getFullYear(), date.getMonth());
		var year = date.getFullYear();
		var day = date.getDate();
		var cld_day = cld[day - 1];
		var festival=[];
		var info = {
			lunar:"",
			y_Info:"",
		};
		info.lunar = '农历' + (cld_day.isLeap ? '闰 ' : '')+templates.getChinaNum(cld_day.lMonth)+"月"+ cDay(cld_day.lDay);
		info.y_Info = cld_day.cYear + '年'+this.lunar_year(date) +" "+ cld_day.cMonth + '月' + cld_day.cDay + '日';
		
		return info;
	},
	lunar_year : function(date){

		var l_year = '【'+ Animals[(date.getFullYear() - 4) % 12] + '年】';
		return l_year;
	},
	getChinaNum :function(Num) {
		var monthEn;
		switch(Num){
			case 1 : monthEn = "一";break;
			case 2 : monthEn = "二";break;
			case 3 : monthEn = "三";break;
			case 4 : monthEn = "四";break;
			case 5 : monthEn = "五";break;
			case 6 : monthEn = "六";break;
			case 7 : monthEn = "七";break;
			case 8 : monthEn = "八";break;
			case 9 : monthEn = "九";break;
			case 10 : monthEn = "十";break;
			case 11 : monthEn = "十一";break;
			case 12 : monthEn = "腊";break;
		}
		return monthEn;
	},

};


window.makeCal = calander;

function getYearWeek(date){  
    var date2=new Date(date.getFullYear(), 0, 1);  
    var day1=date.getDay();  
    if(day1==0) day1=7;  
    var day2=date2.getDay();  
    if(day2==0) day2=7;  
    d = Math.round((date.getTime() - date2.getTime()+(day2-day1)*(24*60*60*1000)) / 86400000);    
    return Math.ceil(d /7)+1;   
}
